﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rdz;
using System.Diagnostics;

namespace Rdz.WindowsConsole
{
	public static class ConsoleCommands
	{
		/// <summary>
		/// Get Switch Arguments on Command Line based on Searched Term. Returning bool.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		public static bool GetSwitchArgs(string SearchedTerm)
		{
			return GetSwitchArgs(SearchedTerm, false);
		}
		/// <summary>
		/// Get Switch Arguments on Command Line based on Searched Term. Returning bool.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		/// <param name="WriteEventLogOnError">True if want to save error to Event Log. Otherwise, false. By default is false.</param>
		public static bool GetSwitchArgs(string SearchedTerm, bool WriteEventLogOnError)
		{
			var CommandItems = Environment.GetCommandLineArgs().Select((item, index) => new { ItemName = item, Position = index });
			try
			{
				if (CommandItems.Where(i => i.ItemName.ToLower() == SearchedTerm.ToLower()).First().Position > 0)
					return true;
				else
					return false;
			} catch (Exception ex)
			{
				if (WriteEventLogOnError)
				{
					EventLog.WriteEntry("Rdz.Console.GetSwitchArgs",
						string.Format(Properties.Resources.Rdz_Console_GetSwitchArgs, SearchedTerm, ex.Message),
						EventLogEntryType.Warning);
				}
				return false;
			}
		}

		/// <summary>
		/// Get String Arguments on Command Line based on Searched Term. Returning string.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		public static string GetStringArgs(string SearchedTerm)
		{
			return GetStringArgs(SearchedTerm, false);
		}

		/// <summary>
		/// Get String Arguments on Command Line based on Searched Term. Returning string.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		/// <param name="WriteEventLogOnError">True if want to save error to Event Log. Otherwise, false. By default is false.</param>
		public static string GetStringArgs(string SearchedTerm, bool WriteEventLogOnError)
		{
			string sRet = "";
			var CommandItems = Environment.GetCommandLineArgs().Select((item, index) => new { ItemName = item, Position = index });
			try
			{
				int Position = CommandItems.Where(i => i.ItemName.ToLower() == SearchedTerm.ToLower()).First().Position;
				return Environment.GetCommandLineArgs()[Position + 1];
			} catch (Exception ex)
			{
				if (WriteEventLogOnError)
				{
					EventLog.WriteEntry("Rdz.Console.GetStringArgs",
						string.Format(Properties.Resources.Rdz_Console_GetStringArgs, SearchedTerm, ex.Message),
						EventLogEntryType.Warning);
				}
			}
			return sRet;
		}

		/// <summary>
		/// Write to Console Command as coloured text, treated as line.
		/// </summary>
		/// <param name="Text">Specifies the text to be shown.</param>
		/// <param name="Colour">Specifies the console colour.</param>
		public static void WriteColored(string Text, ConsoleColor Colour)
		{
			WriteColored(Text, Colour, true);
		}
		/// <summary>
		/// Write to Console Command as coloured text.
		/// </summary>
		/// <param name="Text">Specifies the text to be shown.</param>
		/// <param name="Colour">Specifies the console colour.</param>
		/// <param name="AsLine">Specifies the console text treatment, true if treated as line, otherwise false.</param>
		public static void WriteColored(string Text, ConsoleColor Colour, bool AsLine)
		{
			ConsoleColor DefColor = System.Console.ForegroundColor;
			System.Console.ForegroundColor = Colour;
			if (AsLine)
				System.Console.WriteLine(Text);
			else
				System.Console.Write(Text);
			System.Console.ForegroundColor = DefColor;
		}

		///// <summary>
		///// Writes the specified string value with color to the standard output stream.
		///// </summary>
		///// <param name="Con">System.Console</param>
		///// <param name="Text">The value to write.</param>
		///// <param name="Colour">The color to reflect.</param>
		//public static void WriteColored(this System.Console Con, string Text, ConsoleColor Colour)
		//{
		//    WriteColored(Text, Colour, false);
		//}
		///// <summary>
		///// Writes the specified string value with color, followed by the current line terminator, to the standard output stream.
		///// </summary>
		///// <param name="Con">System.Console</param>
		///// <param name="Text">The value to write.</param>
		///// <param name="Colour">The color to reflect.</param>
		//public static void WriteLineColored(this System.Console Con, string Text, ConsoleColor Colour)
		//{
		//    WriteColored(Text, Colour, true);
		//}

	}
}

#region "Obsolete namespace"
namespace Rdz.Console
{
	public class ConsoleCommands
	{
		/// <summary>
		/// Get Switch Arguments on Command Line based on Searched Term. Returning bool.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		[Obsolete("Use Rdz.WindowsConsole.ConsoleCommands.GetSwitchArgs instead.")]
		public static bool GetSwitchArgs(string SearchedTerm)
		{
			var CommandItems = Environment.GetCommandLineArgs().Select((item, index) => new { ItemName = item, Position = index });
			try
			{
				if (CommandItems.Where(i => i.ItemName.ToLower() == SearchedTerm.ToLower()).First().Position > 0)
					return true;
				else
					return false;
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("Rdz.Console.GetSwitchArgs",
					string.Format(Properties.Resources.Rdz_Console_GetSwitchArgs, SearchedTerm, ex.Message),
					EventLogEntryType.Warning);
				return false;
			}
		}
		/// <summary>
		/// Get String Arguments on Command Line based on Searched Term. Returning string.
		/// </summary>
		/// <param name="SearchedTerm">Terms to be searched.</param>
		[Obsolete("Use Rdz.WindowsConsole.ConsoleCommands.GetStringArgs instead.")]
		public static string GetStringArgs(string SearchedTerm)
		{
			string sRet = "";
			var CommandItems = Environment.GetCommandLineArgs().Select((item, index) => new { ItemName = item, Position = index });
			try
			{
				int Position = CommandItems.Where(i => i.ItemName.ToLower() == SearchedTerm.ToLower()).First().Position;
				return Environment.GetCommandLineArgs()[Position + 1];
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("Rdz.Console.GetStringArgs",
					string.Format(Properties.Resources.Rdz_Console_GetStringArgs, SearchedTerm, ex.Message),
					EventLogEntryType.Warning);
			}
			return sRet;
		}

		private const string WriteColoredObsoleteMsg = "Use Rdz.WindowsConsole.ConsoleCommands.WriteColored instead.";
		/// <summary>
		/// Write to Console Command as coloured text, treated as line.
		/// </summary>
		/// <param name="Text">Specifies the text to be shown.</param>
		/// <param name="Colour">Specifies the console colour.</param>
		[Obsolete(WriteColoredObsoleteMsg)]
		public static void WriteColored(string Text, ConsoleColor Colour)
		{
			WriteColored(Text, Colour, true);
		}
		/// <summary>
		/// Write to Console Command as coloured text.
		/// </summary>
		/// <param name="Text">Specifies the text to be shown.</param>
		/// <param name="Colour">Specifies the console colour.</param>
		/// <param name="AsLine">Specifies the console text treatment, true if treated as line, otherwise false.</param>
		[Obsolete(WriteColoredObsoleteMsg)]
		public static void WriteColored(string Text, ConsoleColor Colour, bool AsLine)
		{
			ConsoleColor DefColor = System.Console.ForegroundColor;
			System.Console.ForegroundColor = Colour;
			if (AsLine)
				System.Console.WriteLine(Text);
			else
				System.Console.Write(Text);
			System.Console.ForegroundColor = DefColor;
		}

	}
}
#endregion
